package com.dbq.fast.business.product.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dbq.fast.R;
import com.dbq.fast.business.coupons.CouponsFragment;
import com.dbq.fast.business.product.adapter.ProductListAdapter;
import com.dbq.fast.business.product.adapter.ServiceCardAdapter;
import com.dbq.fast.business.product.api.ProductApi;
import com.dbq.fast.business.product.bean.ProductType;
import com.dbq.fast.business.product.bean.ServiceType;
import com.dbq.fast.business.product.ui.StatusView;
import com.dbq.fast.common.net.RemoteApiCallBack;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductFragment extends Fragment {
    ServiceType.AppServiceListBean data;
    Bundle args;
    ProductListAdapter adapter;
    private View rootView;

    private ProductType.SetMealListBean checkProduct = null;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private OnCheckItemListener listener;
    private OnLoadStatusListener onLoadListener;

    private boolean isCompete = false;

    public ProductFragment() {

    }

    public boolean getLoadStatus() {
        return isCompete;
    }

    public ProductFragment addParam(String key, Serializable value) {
        if (args == null) {
            args = new Bundle();
        }
        args.putSerializable(key, value);
        return this;
    }

    public ProductFragment build() {
        this.setArguments(args);
        return this;
    }

    public void notifyCloseItem() {
        if (data != null) {
            if (!data.data.isEmpty() && adapter != null) {
                data.data.get(1).isVisible = false;
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.adapter_service_item_view, null);
        ButterKnife.bind(this, rootView);
        Bundle args = getArguments();
        Object extra = args.get("extra");
        if (extra instanceof ServiceType.AppServiceListBean) {
            data = (ServiceType.AppServiceListBean) args.get("extra");
            if (onLoadListener != null) {
                onLoadListener.onStartLoad();
            }
            data.data.clear();
            ProductType.SetMealListBean title = new ProductType.SetMealListBean();
            title.VIEW_TYPE = 1;
            title.ad = data.ad;
            title.title = data.serviceName;
            title.video = data.video;
            title.image = data.image;
            title.speed = data.speed;
            data.data.add(title);

            ProductType.SetMealListBean content = new ProductType.SetMealListBean();
            content.VIEW_TYPE = 2;
            content.content = data.serviceDesc;
            content.isVisible = false;
            data.data.add(content);
            loadData(data);

        }

        adapter = new ProductListAdapter(data.data);
        //GridLayout 3列
        LinearLayoutManager llm = new LinearLayoutManager(getContext());

        recyclerView.setLayoutManager(llm);
        recyclerView.setAdapter(adapter);
        adapter.setOnCheckProductListener((index, data) -> {
            checkProduct = data;
            if (listener != null) {
                listener.onChecked(data);
            }
        });
        return rootView;
    }

    public ProductType.SetMealListBean getCheckedData() {
        return checkProduct;
    }

    public interface OnCheckItemListener {
        void onChecked(ProductType.SetMealListBean data);
    }

    public void setOnCheckedListener(OnCheckItemListener listener) {
        this.listener = listener;
    }

    private void loadData(ServiceType.AppServiceListBean extra) {
        ProductApi.getProductList(extra.serviceId, extra.serviceLevel, new RemoteApiCallBack<ProductType>() {
            @Override
            public void onSuccess(ProductType t) {
                isCompete = true;
                List<ProductType.SetMealListBean> resData = t.setMealList;
                for (int i = resData.size() - 1; i > -1; i--) {
                    extra.data.add(resData.get(i));
                }
                if (!extra.data.isEmpty() && extra.data.size() > 3) {
                    checkProduct = extra.data.get(2);
                    checkProduct.isChecked = true;
                }
                adapter.notifyDataSetChanged();
                if (onLoadListener != null) {
                    onLoadListener.onLoadComplete();
                }
            }

            @Override
            public void onFail(int status, String message) {
                if (onLoadListener != null) {
                    onLoadListener.onLoadError();
                }
            }

        });
    }

    public void setOnLoadListener(OnLoadStatusListener listener) {
        this.onLoadListener = listener;
    }

    public interface OnLoadStatusListener {
        void onStartLoad();

        void onLoadError();

        void onLoadComplete();
    }

}
